const app = getApp();
import Notify from '@vant/weapp/notify/notify';
Page({

  data: {
    educationList: ['在校学生', '中专/技校', '高中', '大专', '专升本', '本科', '硕士', '博士', '其他学历'],
    education: "",
    educationIndex: 0,
    school: "",
    speciality: "",
    education_time: "",
    diploma: [],
    fileToken: '',
    role_id: 3
  },

  onLoad: function () {
    this.getFileToken()
  },

  onShow: function () {

  },

  save() {
    console.log(this.data)
    if (this.data.education == "") {
      Notify({
        type: 'danger',
        message: '请选择教育阶段'
      });
      return false
    }
    if (this.data.school == "") {
      Notify({
        type: 'danger',
        message: '请填写学校'
      });
      return false
    }
    if (this.data.speciality == "") {
      Notify({
        type: 'danger',
        message: '请填写专业'
      });
      return false
    }
    if (this.data.education_time == "") {
      Notify({
        type: 'danger',
        message: '请选择毕业时间'
      });
      return false
    }
    if (this.data.diploma.length < 1) {
      Notify({
        type: 'danger',
        message: '请上传毕业证'
      });
      return false
    }
    let schoolInfo = {
      education: this.data.education,
      school: this.data.school,
      speciality: this.data.speciality,
      education_time: this.data.education_time,
      diploma: this.data.diploma[0].url,
      stu_card: ""
    }
    let post_data = {
      "role_id": this.data.role_id,
      "form": JSON.stringify(schoolInfo)
    }
    var that = this;
    wx.request({
      url: app.globalData.domain + 'interviewer/new_save/?role',
      method: "POST",
      data: post_data,
      header: {
        "Token": app.getToken(),
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      success: function (res) {
        if (res.data.response == "ok") {
          app.setToken(res.data.data.token)
          app.setUserId(res.data.data.user_id)
          app.setRole("用户")
          app.setAdminName(that.data.phone)
          app.setSessionTable("yonghu")
        } else {
          Notify({
            type: 'danger',
            message: res.data.message
          });
        }
      }
    })
  },

  onSchoolChange(e) {
    this.setData({
      education: this.data.educationList[e.detail.value]
    })
    this.setData({
      educationIndex: e.detail.value
    })
  },

  afterRead(event) {
    const {
      file
    } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    var that = this;
    wx.uploadFile({
      url: app.globalData.domain + 'uploads/?upcheck=' + that.data.fileToken + '&up_type=number_id_img', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      // formData: { user: 'test' },
      success(res) {
        var data = JSON.parse(res.data)
        let diploma = []
        let imageInfo = {
          url: app.globalData.domain + "file/" + data.data[0].file_url,
          name: '毕业证',
          isImage: true,
          deletable: true,
        }
        diploma.push(imageInfo)
        that.setData({
          diploma: diploma
        })
      }
    })
  },
  onEducationTimeChange(e) {
    this.setData({
      education_time: this.formatDate(e.detail.value)
    })
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  getFileToken() {
    let that = this
    wx.request({
      url: app.globalData.domain + 'uploads/upload_key/',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        that.setData({
          fileToken: res.data.data.key
        })
      }
    })
  },

  idCardDelete(event) {
    this.setData({
      diploma: []
    })
  },

})